/**
 * Created by oneminute on 7/28/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var path = require("path");
var updateuserphoto = require("../../routes/updateuserphoto");
var userFilter = require("../../routes/userfilter");

describe("测试routes/updateuserphoto模块。", function () {

    var user = null;
    var photo = null;
    before(function (done) {
        user = new lz.User({
            id: lz.Utils.getUUID("u_"),
            name: lz.Utils.getRandName()
        });
        user.save(function(_err, _user) {
            done();
        })
    });

    it("正常上传图片文件。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/updateuserphoto?uid=' + user.id,
            files: {
                photo: {
                    path: path.normalize(__dirname + "/../favicon.png")
                }
            }
        });
        var response = httpMocks.createExpressResponse();

        var getFn = updateuserphoto.post.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.notEqual(_user.photo, null);
                photo = _user.photo;
            });
        });
        userFilter(request, response, getFn);
    });

    it("上传错误类型的图片文件。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/updateuserphoto?uid=' + user.id,
            files: {
                photo: {
                    path: module.filename
                }
            }
        });
        var response = httpMocks.createExpressResponse();

        var getFn = updateuserphoto.post.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.equal(_err.code, lz.Config.errors.IMAGE_ERROR.code);
            });
        });
        userFilter(request, response, getFn);
    });

    after(function (done) {
        user.delete(function() {
            lz.ImageManager.removePhotos(photo, function() {
                done();
            });
        });
    });
});